﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Palc.XMPP;
using NUnit.Framework;

namespace Palc.XMPP.Test
{
    [TestFixture]
    public class JabberIdentifierTest
    {

        const int JIDPartMaxLength = 1023;

        [Test]
        public void CreateDomain()
        {
            JabberIdentifier jid = new JabberIdentifier();
            jid.Domain = "palc.de";
            
        }

        [Test]
        [ExpectedException("Palc.XMPP.Exceptions.JabberIdentifierException")]
        public void CreateInvalidDomain()
        {
            JabberIdentifier jid = new JabberIdentifier();
            jid.Domain = "";
        }
        
        [Test]
        public void CreateValidJabberIdentifier() 
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.FullAddress = "admin@mars/voyager";

            Assert.AreEqual("admin", jid.Node);
            Assert.AreEqual("voyager", jid.Resource);
            Assert.AreEqual("mars", jid.Domain);
        }

        [Test]
        [ExpectedException( "Palc.XMPP.Exceptions.JabberIdentifierException" )]
        public void CreateInvalidJabberIdentifier()
        {
            JabberIdentifier jid = new JabberIdentifier();

            // This jid has mixed up the / and @
            jid.FullAddress = "admin/mars@voyager";

            Assert.AreEqual("admin", jid.Node);
            Assert.AreEqual("mars", jid.Domain);
            Assert.AreEqual("voyager", jid.Resource);
        }

        [Test]
        public void CreateALargeJID()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.FullAddress = ("".PadRight(JIDPartMaxLength, 'n') + JabberIdentifier.NodeSeparator + "".PadRight(JIDPartMaxLength, 'd') + JabberIdentifier.ResourceSeparator + "".PadRight(JIDPartMaxLength, 'r'));
        }

        [Test]
        [ExpectedException("Palc.XMPP.Exceptions.JabberIdentifierException")]
        public void CreateATooLargeJID()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.FullAddress = ("a" + "".PadRight(JIDPartMaxLength, 'n') + JabberIdentifier.NodeSeparator + "".PadRight(JIDPartMaxLength, 'd') + JabberIdentifier.ResourceSeparator + "".PadRight(JIDPartMaxLength, 'r'));
        }

        [Test]
        public void CreateALargeJIDNode()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.Node = ("".PadRight(JIDPartMaxLength, 'n'));
            Assert.AreEqual("".PadRight(JIDPartMaxLength, 'n'), jid.Node);
        }

        [Test]
        [ExpectedException("Palc.XMPP.Exceptions.JabberIdentifierException")]
        public void CreateATooLargeJIDNode()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.Node = ("a" + "".PadRight(JIDPartMaxLength, 'n'));
        }

        [Test]
        public void CreateALargeJIDResource()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.Resource = ("".PadRight(JIDPartMaxLength, 'n'));
            Assert.AreEqual("".PadRight(JIDPartMaxLength, 'n'), jid.Resource);
        }

        [Test]
        [ExpectedException("Palc.XMPP.Exceptions.JabberIdentifierException")]
        public void CreateATooLargeJIDResource()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.Resource = ("a" + "".PadRight(JIDPartMaxLength, 'n'));
        }

        [Test]
        public void CreateALargeJIDDomain()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.Domain = ("".PadRight(JIDPartMaxLength, 'n'));
            Assert.AreEqual("".PadRight(JIDPartMaxLength, 'n'), jid.Domain);

        }

        [Test]
        [ExpectedException("Palc.XMPP.Exceptions.JabberIdentifierException")]
        public void CreateATooLargeJIDDomain()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.Domain = ("a" + "".PadRight(JIDPartMaxLength, 'n'));
        }

        [Test]
        [ExpectedException("Palc.XMPP.Exceptions.JabberIdentifierException")]
        public void CreateAnInvalidJIDDomain()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.Domain = ("do/ma@in");
        }

        [Test]
        [ExpectedException("Palc.XMPP.Exceptions.JabberIdentifierException")]
        public void CreateAnInvalidJIDResource()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.Resource = ("re/source");
        }

        [Test]
        [ExpectedException("Palc.XMPP.Exceptions.JabberIdentifierException")]
        public void CreateAnInvalidJIDNode()
        {
            JabberIdentifier jid = new JabberIdentifier();

            jid.Node = ("no@de");
        }

        [Test]
        [ExpectedException("Palc.XMPP.Exceptions.JabberIdentifierException")]
        public void CreateAJIDWithInvalidCharacters()
        {
            JabberIdentifier jid = new JabberIdentifier();
            //TODO Test with parameters possible?
            //jid.FullAddress = ("do\"main");
            //jid.FullAddress = ("dom&ain");
            //jid.FullAddress = ("dom:ain");
            //jid.FullAddress = ("dom<ain");
            jid.FullAddress = ("dom>ain"); 
        }

        //TODO Create Test with IPv4 and IPv6 addresses as Domain
    }
}
